<?php 
//活动支付回调
include('./fun.php');
$conn=my_sql($cfg);
$set=get_set();
$ac='get';
if($ac=='get'){
$post_data = file_get_contents('php://input');
if(!$post_data){exit;}
$posts=xmlToArray($post_data);
}else{//支付测试
//$arr['data']=$post_data;
//insert($cfg['e'].'pay_back',$arr);
//exit;
$post_data='<xml><appid><![CDATA[wx77a7fa0545f2bbc2]]></appid>
<attach><![CDATA[59_0cd0289f1884b3767f097c6cbe92c5c2]]></attach>
<bank_type><![CDATA[OTHERS]]></bank_type>
<cash_fee><![CDATA[19]]></cash_fee>
<fee_type><![CDATA[CNY]]></fee_type>
<is_subscribe><![CDATA[N]]></is_subscribe>
<mch_id><![CDATA[1508007161]]></mch_id>
<nonce_str><![CDATA[1665847936]]></nonce_str>
<openid><![CDATA[orLYC5c87AEbg28mTUIbV6UqnesA]]></openid>
<out_trade_no><![CDATA[59_1665847936]]></out_trade_no>
<result_code><![CDATA[SUCCESS]]></result_code>
<return_code><![CDATA[SUCCESS]]></return_code>
<sign><![CDATA[C0C7953594CA21143E34E8ED49260E46]]></sign>
<time_end><![CDATA[20221015233230]]></time_end>
<total_fee>19</total_fee>
<trade_type><![CDATA[JSAPI]]></trade_type>
<transaction_id><![CDATA[4200001585202210154955755574]]></transaction_id>
</xml>';

$posts=xmlToArray($post_data);//print_r($posts);
}


if($posts['result_code']!='SUCCESS'){//支付失败
wlog(array('c'=>'pay_back','s'=>'支付回调错误','msg'=>$posts['result_code']));exit;
}

$o_id=$posts['out_trade_no'];
$x=explode('_',$o_id);
$order_id=$x[0];//订单号

if($ac=='get'){
$sql='select * from '.$cfg['e'].'pay_back where  order_id="'.a($order_id).'" ';//订单
$ck=db1($sql);if($ck){echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';exit;}//该返回数据已经存在

$arr['order_id']=$order_id;
$arr['date']=time();
$arr['data']=$post_data;

$data_id=insert($cfg['e'].'pay_back',$arr);//将数据添加到data
}else{
echo '订单编号:'.$o_id.'<br>';
}
unset($arr);

//获取订单

$sql='select * from '.$cfg['e'].'order where id="'.a($order_id).'" ';
if($ac=='get'){$sql.='and state=0';}
$order=db1($sql);

if(!$order){echo '查找订单失败';
wlog(array('c'=>'pay_back','s'=>'没有找到订单','msg'=>'订单号:'.$order_id));exit;}


//print_r($posts);

$arr['pay_price']=$posts['total_fee'];//订单信息
$arr['pay_date']=time();
$arr['transaction_id']=$posts['transaction_id'];//微信订单号
$arr['state']=1;//$arr['out_trade_no']=$posts['out_trade_no'];//订单号
$arr['coupon_fee']=$posts['coupon_fee'];//优惠券金额
$arr['cash_fee']=$posts['cash_fee'];


if(!$arr['coupon_fee']){$arr['coupon_fee']=0;}


if($ac=='get'){
    update($cfg['e'].'order',$arr,' where id="'.$order['id'].'"');//首先保存下订单状态
    //订单支付完成后打印小票
    printer($order['id']);

}else{
echo '订单保存信息:<br>';
print_r($arr);
echo '支付时间'.date('Y-m-d H:i:s',$arr['pay_date']);
echo '<br>';
}
$money=$posts['total_fee']/100;//金额

//$adm
//$sql='select * from '.$cfg['e'].'adm where id="'.$order['adm_id'].'"';
//$adm=db1($sql);
//$adm_arr['order_num']=$adm['order_num']+1;//订单数
//$adm_arr['order_price']=$adm['order_price']+$money;//订单金额
//$adm_arr['money']=$adm['money']+$money;//金额


//门店
$sql='select * from '.$cfg['e'].'shop where  id="'.$order['shop_id'].'"';
$shop=db1($sql);

//活动
$sql='select * from '.$cfg['e'].'hd where id="'.$order['hd_id'].'" ';
$hd=db1($sql);
if(!$hd){wlog(array('c'=>'pay_back','s'=>'没有找到活动','msg'=>'订单号:'.$order_id));exit;}


//$shop_arr['order_num']=$shop['order_num']+1;//门店订单数
//$shop_arr['order_price']=$shop['order_price']+$money;//门店金额
//$shop_arr['balance']=$shop['balance']+$money;

//账单
$zd['shop_id']=$shop['id'];
$zd['date']=time();
$zd['order_id']=$order['id'];
//管理员账单
$zd['user_id']=$shop['adm_id'];
$zd['title']='商品支付收入';
$zd['c']='pay';
$zd['money']=$money;//金额
$zd['nickname']=$order['nickname'];
$zd['logo']=$order['logo'];
//$zd['balance']=$shop['money']+$money;//余额
$zd['state']=0;
if($ac=='get'){
zd($zd);//用户账单
zd_balance($shop);//对账单进行统计
}else{
echo '管理员门店账单:';print_r($zd);echo '<br>';
}

//用户
$sql='select * from '.$cfg['e'].'users where id="'.$order['user_id'].'" ';
$user=db1($sql);






//用户积分
$set['jifen']=0;//取消积分
if($set['jifen']>0){
$jifen_num=ceil($money*$set['jifen']);
$user_arr['jifen']=$user['jifen']+$jifen_num;//用户积分
$user_update=1;//更新

$jifen_arr['balance']=$user_arr['jifen'];//积分余额
$jifen_arr['adm_id']=$adm['id'];
$jifen_arr['user_id']=$user['id'];
$jifen_arr['date']=time();
$jifen_arr['title']='购物';
$jifen_arr['c']='get';
$jifen_arr['jifen']=$jifen_num;
if($ac=='get'){
insert($cfg['e'].'jifen',$jifen_arr);//添加到积分目录
}else{
echo '积分:<br>';
print_r($jifen_arr);echo '<br>';
}
}




if($user_update){//更新用户
if($ac=='get'){
update($cfg['e'].'users',$user_arr,' where id="'.$user['id'].'"');}else{
echo '用户信息：<br>';print_r($user_arr);echo '<br>';
}
}

if($ac=='get'){//更新商户 商店
//update($cfg['e'].'adm',$adm_arr,' where id="'.$adm['id'].'"');
//update($cfg['e'].'shop',$shop_arr,' where id="'.$shop['id'].'"');

//订单数添加
$sql='update '.$cfg['e'].'shop set order_num=order_num+1 where id="'.$shop['id'].'"';
sql_query($sql);

}else{
//echo '管理员保存：<br>';print_r($adm_arr);echo '<br>';
//echo '门店信息：<br>';print_r($shop_arr);echo '<br>';

}



echo '活动内容<br>';
print_r($hd);echo '<br>';


$hd_arr['order_num']=$hd['order_num']+1;
$hd_arr['people_num']=$hd['people_num']+1;



//商品库存
$sql='select * from '.$cfg['e'].'order_pro where order_id="'.$order['id'].'" ';
$order_pro=db($sql);
if(!$order_pro){wlog(array('c'=>'pay_back','s'=>'没有找到活动商品','msg'=>'订单号:'.$order_id));exit;}//对商品进行分析
foreach($order_pro as $v){
switch($v['c']){
case 'pro':
$cache_key=$v['cache_key'];//缓存的    
$sell=$v['num'];
if($cache_key){
$pro=$pros[$v['pro_id']];
if(!$pro){//查找商品
$sql='select * from '.$cfg['e'].'pro where id="'.$v['pro_id'].'" ';
$pro=db1($sql);
}

if($pro['data']){
$pro['data']=unserialize(out($pro['data']));


echo '商品参数';print_r($pro['data']);echo '<br>';

$temp_c=explode('_',$cache_key);

echo '关键词：';print_r($temp_c);echo '<br><br>';

if(is_numeric($temp_c[3])){//存在2级
$pro['data'][$temp_c[2]]['item'][$temp_c[3]]['num']=$pro['data'][$temp_c[2]]['item'][$temp_c[3]]['num']-$sell;
if($pro['data'][$temp_c[2]]['item'][$temp_c[3]]['num']<=0){$pro['data'][$temp_c[2]]['item'][$temp_c[3]]['num']=0;}
//print_r($temp_c[2]);
}else{
$pro['data'][$temp_c[2]]['num']=$pro['data'][$temp_c[2]]['num']-$sell;
if($pro['data'][$temp_c[2]]['num']<=0){$pro['data'][$temp_c[2]]['num']=0;}
}

}else{//如果没有属性
if($pro['total']!=-1){
$pro['total']=$pro['total']-$sell;
if($pro['total']<=0){$pro['total']=0;}
}
}



if($pro['data']){$pro['data']=serialize($pro['data']);}
$pros[$pro['id']]=$pro;//缓存
}



break;//商品
case '秒杀':
$pro_num=$hd['miao_total'];
if($pro_num!=-1){
$hd_arr['miao_total']=$pro_num-1;
if($hd_arr['miao_total']<0){$hd_arr['miao_total']=0;}
}
break;
case '赠品'://赠品
$pro_num=$hd['mz_total'];
if($pro_num!=-1){
$hd_arr['mz_total']=$pro_num-1;
if($hd_arr['mz_total']<0){$hd_arr['mz_total']=0;}
}
break;    
    
}
}


$pro_data_arr['state']=1;
if($ac=='get'){
update($cfg['e'].'order_pro',$pro_data_arr,'where order_id="'.$order['id'].'"');
}else{
echo '商品订单数据:';print_r($pro_data_arr);echo '<br>';
}


if($pros){//保存商品信息
print_r($pros);


//保存商品信息
foreach($pros as $v){
unset($pro_arr);
if($v['data']){
$pro_arr['data']=$v['data'];
$pro_arr['total']=pro_num(unserialize(out($v['data'])));//商品数量

}else{
$pro_arr['total']=$v['total'];
}
if($ac=='get'){
update($cfg['e'].'pro',$pro_arr,' where id="'.$v['id'].'"');
}else{
echo '保存商品数据';print_r($pro_arr);echo $v['id'].'<br>';
}
}

}

//保存活动
if($hd_arr){
if($ac=='get'){
update($cfg['e'].'hd',$hd_arr,' where id="'.$hd['id'].'"');
}else{
echo '活动信息保存';print_r($hd_arr);echo '<br>';
}
}


//优惠券
if($order['quan_id']){
$quan_array['state']=2;
$quan_array['is_use']=time();
$quan_array['order_id']=$order['id'];
if($ac=='get'){
update($cfg['e'].'quan_get',$quan_array,' where id="'.$order['quan_id'].'"');
}else{
echo '优惠券';print_r($quan_array);echo '<br>';
}
}





//update($cfg['e'].'order',$arr,' where id="'.$order['id'].'"');

$sql='select * from '.$cfg['e'].'users where shop_id="'.$shop['id'].'"';
$uu=db1($sql);

if($uu['wx_name']){
//新订单通知 是否发送给更多的管理员
$d['path']='/pages/order/index';
$d['query']='id='.$order['id'];
$url='';
$re=get_url($d);
$re=json_decode($re,1);
$url=$re['url_link'];

$msg['touser']=$uu['wx_name'];
$msg['temp_id']=trim($cfg['msg']['新订单']['temp_id']);
$msg['url']=$url;
$msg_data['first']='您有新的订单';
$msg_data['keyword1']['value']=date('Y-m-d H:i:s',$order['time']);
$msg_data['keyword2']['value']=number_format($order['total'],2).'元';
$msg_data['keyword3']['value']='共'.$order['num'].'件商品,支付金额：'.number_format($arr['pay_price']/100,2,'.','');
$msg_data['keyword4']['value']='微信支付 配送方式:'.$order['yun'];
$msg_data['remark']['value']='请速安排商品，进行配送/发货。';
$msg_data['remark']['color']='red';
$msg['data']=$msg_data;
$re=wx_msg($msg);
print_r($msg);
print_r($re);


}
//发货信息录入
sleep(5);
upload_shipping_info($order,$posts['transaction_id'],$user);
//小程序消息


///////////////////////////////////////////////




function zd($d){//账单
global $conn,$cfg;
$i=insert($cfg['e'].'pay',$d);
}


//微信消息

function new_order_msg($users){
global $order,$user;global $cfg;global $config;
$url='https://www.zjhn.com/dc/index.php';
$temp_id='i5qdpEQhuYpo1Bbm0WwkVKbYg4etEIAF3i1yPiAFyhY';
$data_arr['first']='您有新的支付订单';
$data_arr['keyword1']['value']='微信支付';
$data_arr['keyword1']['color']='#ff4444';
$data_arr['keyword2']['value']='支付金额￥'.number_format($o['pay_price']/100,2);
$data_arr['keyword2']['color']='';
$data_arr['keyword3']['value']=$user['nickname'].' '.$user['phone'];
$data_arr['keyword3']['color']='';
$data_arr['keyword4']['value']=date('Y-m-d H:i:s',time());
$data_arr['keyword4']['color']='';
$data_arr['keyword5']['value']=$order['order_id'];
$data_arr['keyword5']['color']='';


$data_arr['remark']['value']='请到管理后台查看订单';
$data_arr['remark']['color']='green';

if($users){
$uu=unserialize(out($users));
if($uu){
foreach($uu as $k=>$v){
$post_data = array (
  "touser"           =>$k,
  "template_id"      => $temp_id,
  "url"             => $url,'topcolor'=>'#000',
  "data"             => $data_arr
);


$token=getAccessToken ($config['appID'], $config['appsecret']);  
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
//这里替换为你的 appID 和 appSecret
$data = json_encode($post_data, true); 
//print_r($data);
//将数组编码为 JSON
 
$return = send_post( $url, $data);

}

}
}

}




function new_order($users){
global $orders,$total,$user,$arr;global $cfg;global $config;
$url='https://www.zjhn.com/dc/index.php';
$temp_id='i5qdpEQhuYpo1Bbm0WwkVKbYg4etEIAF3i1yPiAFyhY';
$data_arr['first']='您有新的点餐订单';
$data_arr['keyword1']['value']=$orders;
$data_arr['keyword1']['color']='#ff4444';
$data_arr['keyword2']['value']='￥'.number_format($total,2);
$data_arr['keyword2']['color']='';
$data_arr['keyword3']['value']=$user['nickname'];
$data_arr['keyword3']['color']='';
$data_arr['keyword4']['value']=date('Y-m-d H:i:s');
$data_arr['keyword4']['color']='';
$data_arr['keyword5']['value']=$arr['order_id'];
$data_arr['keyword5']['color']='';


$data_arr['remark']['value']='请到管理后台查看订单';
$data_arr['remark']['color']='green';

if($users){
$uu=unserialize(out($users));
if($uu){
foreach($uu as $k=>$v){
$post_data = array (
  "touser"           =>$k,
  "template_id"      => $temp_id,
  "url"             => $url,'topcolor'=>'#000',
  "data"             => $data_arr
);


$token=getAccessToken ($config['appID'], $config['appsecret']);  
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
//这里替换为你的 appID 和 appSecret
$data = json_encode($post_data, true); 
//print_r($data);
//将数组编码为 JSON
 
$return = send_post( $url, $data);

}


}
}

}







?>